//
//  HMComposePictureView.swift
//  HMSlender
//
//  Created by 赵少朋赵少朋 on 2017/2/11.
//  Copyright © 2017年 ITheima. All rights reserved.
//

import UIKit

class HMComposePictureView: UICollectionView {

    let cellID = "cellId"
    /// 点击+号按钮的闭包
    var closure:(()->())?
    
    
    // 提供保存图片数组
    var imageArr:[UIImage] = [UIImage]()
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        let itemHW = (SCREENWIDTH - 20 - 10) / 3
        flowLayout.itemSize = CGSize(width: itemHW , height: itemHW)
        flowLayout.minimumLineSpacing = 5
        flowLayout.minimumInteritemSpacing = 5
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() -> Void {
        
        backgroundColor = UIColor.white
        self.isHidden = true
        dataSource = self
        delegate = self
        
        register(HMComposePictureViewCell.self, forCellWithReuseIdentifier: cellID)
    }
    
    // 提供给外界添加图片方法
    func addImage(img:UIImage) -> Void {
        
        //显示配图
        isHidden = false
        // 把图片放大最前面
        imageArr.insert(img, at: 0)
        if imageArr.count > 9 {
            imageArr.removeLast()
        }
        
        self.reloadData()
    }
}

extension HMComposePictureView:UICollectionViewDataSource {

    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let count = imageArr.count
        if count == 0 || count == 9 {
            
            return count
        }
        
        return imageArr.count + 1
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellID, for: indexPath) as! HMComposePictureViewCell
        
        //
        cell.deleteClosure = {[weak self] ()->() in
            //删除对应的数据
            self?.imageArr.remove(at: indexPath.row)
            // 刷新数据
            self?.reloadData()
            if self?.imageArr.count == 0 {
                self?.isHidden = true
            }
        }
        // 如果下标 = 图片数组总个数 表示最后一张
        if indexPath.item == imageArr.count {
            
            cell.image = nil
        } else {
            cell.image = imageArr[indexPath.row]
        }
        
        return cell
    }
}

/// 代理方法
extension HMComposePictureView:UICollectionViewDelegate {

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        if indexPath.item == imageArr.count {
            
            closure?()
            
        }
    }
    
}

//自定义cell
class HMComposePictureViewCell: UICollectionViewCell {

    //定义image属性让外界给它赋值
    var image:UIImage? {
    
        didSet {
            if image == nil {
                
                bgImageView.image = UIImage(named: "compose_pic_add")
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                deleteBtn.isHidden = true
                
            } else {
                bgImageView.image = image
                bgImageView.highlightedImage = nil
                deleteBtn.isHidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() -> Void {
        
        //添加背景图片
        addSubview(bgImageView)
        addSubview(deleteBtn)
        
        // 添加约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
        deleteBtn.snp_makeConstraints { (make) in
            make.right.top.equalTo(contentView)
        }
        
    }
    /// 删除按钮闭包
    var deleteClosure:(()->())?
    
    //背景图片
    private lazy var bgImageView: UIImageView = UIImageView()
    // 右上角删除按钮
    private lazy var deleteBtn: UIButton = {
    
        let btn = UIButton()
        btn.setImage(UIImage(named:"compose_photo_close"), for: UIControlState.normal)
        btn.isHidden = true
        btn.addTarget(self, action: #selector(deleteBtnClick), for: UIControlEvents.touchUpInside)
        return btn
    }()
    
    
    func deleteBtnClick(button:UIButton) -> Void {
        // 调用闭删除按钮闭包
        deleteClosure?()
        
    }
    
}

